package com.ease.integration.telent;

import com.ease.common.util.Detect;

public class TelnetTemplate extends Detect {

	private TelnetClient telnetClient = null;

	private boolean connect() throws RuntimeException {
		return telnetClient.connect();
	}

	private boolean disconnect() {
		return telnetClient.disconnect();
	}

	public boolean login() throws RuntimeException {
		return telnetClient.login();
	}

	public <T> T execute(TelnetExecutionCallback<T> telnetExecutionCallback) throws RuntimeException {
		try {
			this.connect();
			this.login();
			return telnetExecutionCallback.execute(telnetClient);
		} catch (Exception e) {
			// TODO
			e.printStackTrace();
		} finally {
			this.disconnect();
		}
		return (T) null;
	}

	// public String send(final String command) throws RuntimeException {
	//
	// return this.execute(new TelnetExecutionCallback<String>() {
	//
	// public String execute(TelnetClient telnetClient) {
	// return telnetClient.send(command);
	// }
	//
	// });
	// }

	/**
	 * @return the telnetClient
	 */
	public TelnetClient getTelnetClient() {
		return telnetClient;
	}

	/**
	 * @param telnetClient
	 *            the telnetClient to set
	 */
	public void setTelnetClient(TelnetClient telnetClient) {
		this.telnetClient = telnetClient;
	}

}
